use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

// 用户模型
#[derive(Debug, Clone)]
pub struct User {
    pub id: String,
    pub username: String,
    pub email: String,
    pub password_hash: String,
    pub created_at: OffsetDateTime,
    pub updated_at: OffsetDateTime,
    pub is_active: bool,
}

// 用于序列化的用户模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserResponse {
    pub id: String,
    pub username: String,
    pub email: String,
    pub created_at: String,
    pub updated_at: String,
    pub is_active: bool,
}

impl From<User> for UserResponse {
    fn from(user: User) -> Self {
        Self {
            id: user.id,
            username: user.username,
            email: user.email,
            created_at: user.created_at.to_string(),
            updated_at: user.updated_at.to_string(),
            is_active: user.is_active,
        }
    }
}

// API响应模型
#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse<T> {
    pub code: u32,
    pub message: String,
    pub data: Option<T>,
}

impl<T> ApiResponse<T> {
    pub fn success(data: T) -> Self {
        Self {
            code: 200,
            message: "success".to_string(),
            data: Some(data),
        }
    }
    
    pub fn error(message: String) -> Self {
        Self {
            code: 500,
            message,
            data: None,
        }
    }
    
    pub fn unauthorized() -> Self {
        Self {
            code: 401,
            message: "unauthorized".to_string(),
            data: None,
        }
    }
}